package com.example.pda.ui.list;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.pda.R;
import com.example.pda.adapter.OrderListAdapter;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.BaseViewModel;
import com.example.pda.common.CommonCallback;
import com.example.pda.common.Utils;
import com.example.pda.data.model.GoodItem;
import com.example.pda.data.model.Inventory;
import com.example.pda.databinding.ActivityOrderListBinding;
import com.example.pda.common.CompatUtil;
import com.example.pda.ui.stock.StockActivity;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class OrderListActivity extends BaseActivity {

    private ActivityOrderListBinding binding;
    private OrderListViewModel orderListViewModel;
    private OrderListAdapter adapter;

    private String mType;
    private String mTitle;

    public static void start(Context context, String type, String name) {
        Intent intent = new Intent(context, OrderListActivity.class);
        intent.putExtra("TYPE", type);
        intent.putExtra("TITLE", name);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_order_list);
        binding.setLifecycleOwner(this);
        orderListViewModel = new ViewModelProvider(this, new OrderListViewModelFactory())
                .get(OrderListViewModel.class);
        registerObserve(orderListViewModel);

        mType = getIntent().getStringExtra("TYPE");
        mTitle = getIntent().getStringExtra("TITLE");

        binding.abc.getTitleTextView().setText(mTitle);

        initSwipeRefreshLayout();
        initRecycler();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Utils.isEmpty(orderListViewModel.getDjhData().getValue())) {
            orderListViewModel.loadOrder(true, mType);
        }
    }

    private void initSwipeRefreshLayout() {
        binding.swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent);
        binding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                orderListViewModel.loadOrder(false, mType);
            }
        });
    }

    private Inventory mCurInv;

    private void initRecycler() {
        adapter = new OrderListAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
        adapter.setListener(new CommonCallback<Inventory>() {
            @Override
            public void onResult(Inventory data, Object... objects) {
                mCurInv = data;
                orderListViewModel.loadDjhItmByDjh(data.getDjh());
            }
        });

        orderListViewModel.mDataBean.observe(this, new Observer<List<GoodItem>>() {
            @Override
            public void onChanged(List<GoodItem> items) {
                CompatUtil.getInstance().setItemList(items);
                if (!Utils.isEmpty(items)) {
                    CompatUtil.getInstance().updateLocalData(mCurInv);
                    StockActivity.start(OrderListActivity.this, mCurInv);
                } else {
                    Utils.getInstance().showToast("接口异常，返回数据为空");
                }
            }
        });
    }

    @Override
    protected void registerObserve(BaseViewModel viewModel) {
        super.registerObserve(viewModel);
        orderListViewModel.getDjhData().observe(this, new Observer<List<Inventory>>() {
            @Override
            public void onChanged(List<Inventory> djhBeans) {
                adapter.setData(djhBeans);
                binding.swipeRefreshLayout.setRefreshing(false);

                if (Utils.isEmpty(djhBeans)) {
                    binding.includeEmpty.emptyLayout.setVisibility(View.VISIBLE);
                } else {
                    binding.includeEmpty.emptyLayout.setVisibility(View.GONE);
                }
            }
        });
    }
}
